package com.polaris.live.adapter.level

import android.view.LayoutInflater
import android.view.View
import com.bumptech.glide.Glide
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.AdapterMeRichCharmBinding
import com.polaris.live.databinding.LayoutMeRichCharmItemBinding
import com.polaris.live.resp.back_resp.UserRichCharmLevelConfigVo
import com.polaris.live.resp.back_resp.UserRichCharmLevelRewardExplainVo
import com.polaris.live.utils.image.ImageUtils

/**
 * MeRichCharmAdapter
 *
 * @author Created by 半仙 on 2024/9/18/018 17:08
 */
class MeRichCharmAdapter : BaseBindingAdapter<AdapterMeRichCharmBinding, UserRichCharmLevelConfigVo>() {

    private var mType = AppConst.UserSex.MAN

    override fun convert(holder: VBViewHolder<AdapterMeRichCharmBinding>, item: UserRichCharmLevelConfigVo) {
        holder.vb.apply {
            if (mType == AppConst.UserSex.MAN) {
                levelView.setNormalRichsLevel(item.level)
            } else {
                levelView.setNormalCharmLevel(item.level)
            }
            tvDiamond.text = item.score.toString()
            val rewards = item.rewards
            itemLl.removeAllViews()
            if (rewards.isNotEmpty()) {
                rewards.forEach {
                    itemLl.addView(getMView(it))
                }
            }
        }
    }

    private fun getMView(d: UserRichCharmLevelRewardExplainVo): View {
        val binding = LayoutMeRichCharmItemBinding.inflate(LayoutInflater.from(context), null, false)
        binding.root.setOnSingleClickListener {
            back?.invoke(d)
        }
        binding.apply {
            if (d.rewardType == 0L) {
                tvMessage.text = d.title
                image.visibility = View.GONE
            } else {
                tvMessage.visibility = View.GONE
                Glide.with(context)
                    .load(ImageUtils.imgPath(d.photoUrl))
                    .into(image)
            }
        }
        return binding.root
    }

    fun setType(type: Int) {
        mType = type
        notifyDataSetChanged()
    }

    private var back: ((data: UserRichCharmLevelRewardExplainVo) -> Unit)? = null
    fun setClickItem(back: (data: UserRichCharmLevelRewardExplainVo) -> Unit) {
        this.back = back
    }

}